package com.nexd.location.employee.presenter;

import android.content.Context;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.nexd.location.employee.Config;
import com.nexd.location.employee.ui.iView.INexdLocationView;
import com.nexd.location.employee.util.BasicPhoneInformation;

import cn.nexd.sdk.location.NexdIndoorLocation;
import cn.nexd.sdk.location.NexdIndoorLocationAgent;
import cn.nexd.sdk.location.NexdIndoorLocationConfiguration;
import cn.nexd.sdk.location.NexdIndoorLocationListener;
import cn.nexd.sdk.location.NexdIndoorLocationResult;

/**
 * Created by wangxu on 16/4/26.
 */
public class NexdLocationPresenter extends BasePresenter<INexdLocationView> {

    private static final String TAG = NexdLocationPresenter.class.getSimpleName();
    private static final String UPLOAD_LOCATION_COORDINATE = "upload_location_coordinate";
    //    private NexdIndoorLocationAgent nexdIndoorLocationAgent;
    private NexdIndoorLocationAgent nexdLocationAgent;
    private RequestQueue requestQueue;//这里的this指的是Context

    public NexdLocationPresenter(Context context, INexdLocationView iView) {
        super(context, iView);
        nexdLocationAgent = NexdIndoorLocationAgent.getIndoorLocationAgent(context);
        requestQueue = Volley.newRequestQueue(context);
    }

    public void initLocationBuilder() {

        NexdIndoorLocationConfiguration.Buidler buidler = new NexdIndoorLocationConfiguration.Buidler();
        buidler.setAppkey("").setLocationMode(NexdIndoorLocation.LocationMode.LOCATION_MODE_BEACON_ONLY);
        buidler.setLocationRate(1000);
        buidler.setBeaconResultCacheTime(5000);
        buidler.setBeaconCollectorRate(1000);

        nexdLocationAgent.startLocation(10108058L, 101080580001L, new NexdIndoorLocationListener() {
            @Override
            public void onLocationStart() {
                Log.d(TAG, "onLocationStart: 开始定位");
            }

            @Override
            public void onLocationChanged(NexdIndoorLocation nexdIndoorLocation, NexdIndoorLocationResult nexdIndoorLocationResult) {
                if (nexdIndoorLocation.getLocationEngineState().equals(NexdIndoorLocation.LocationEngineState.ERROR_CODE_LOCATION_SUCCESS)) {
                    iView.updateLocationCoordinate(nexdIndoorLocationResult.getIndoorPositionX(), nexdIndoorLocationResult.getIndoorPositionY());
                    uploadLocationCoordinate(nexdIndoorLocationResult.getIndoorPositionX(), nexdIndoorLocationResult.getIndoorPositionY());
                } else {
                    iView.showToast("定位失败!");
                }
            }
        }, buidler.build());

    }


    // 上传定位点, 定位点是没有计算偏移量的
    private void uploadLocationCoordinate(float x, float y) {
        StringBuilder url = new StringBuilder();
        url.append(Config.BASE_IP)
                .append("?appKey=")
                .append(Config.APP_KEY)
                .append("&phoneCode=")
                .append(BasicPhoneInformation.getInstance(context).getDeviceId())
                .append("&x=")
                .append(String.valueOf(x))
                .append("&y=")
                .append(String.valueOf(y));
        Log.d(TAG, "uploadLocationCoordinate: " + url.toString());

        StringRequest uploadLocationCoordinate = new StringRequest(Request.Method.POST, url.toString(), new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        cancelPendingRequests(UPLOAD_LOCATION_COORDINATE);
        uploadLocationCoordinate.setRetryPolicy(new DefaultRetryPolicy(1000, 0, 0f));
        uploadLocationCoordinate.setTag(UPLOAD_LOCATION_COORDINATE);
        requestQueue.add(uploadLocationCoordinate);
    }

    // 通过各Request对象的Tag属性取消请求
    private void cancelPendingRequests(Object tag) {
        if (requestQueue != null) {
            requestQueue.cancelAll(tag);
        }
    }
}
